package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.packers.TeamDetailEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author younghow
 * @date 2020/8/23
 * description:工队首页下半部分的子fragment
 */
public class TeamDetailViewModel extends BaseViewModel {

    public MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> successLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> errorLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> mFollowSuccessLiveData = new MutableLiveData<>();
    public MutableLiveData<TeamDetailEntity> mTeamDetailLiveData = new MutableLiveData<>();
    public MutableLiveData<String> mImLiveData = new MutableLiveData<>();
    public MutableLiveData<String > mImbLiveData = new MutableLiveData<>();

    public TeamDetailViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void getTeamDetail(String no){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTeamDetail(no).enqueue(new Callback<BaseEntity<TeamDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<TeamDetailEntity>> call, Response<BaseEntity<TeamDetailEntity>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mTeamDetailLiveData.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<TeamDetailEntity>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void followForTeam(String no,String type, boolean isAttention) {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().follow(isAttention,no,Integer.parseInt(type)).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowSuccessLiveData.postValue(true);
                }else {
                    if (response.body().getMsg().contains("数据不存在")){
                        mFollowSuccessLiveData.postValue(false);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void findCustomerServiceImAccount(String imId) {
        loadingLiveData.postValue(true);
        Map<String,Object> map = new HashMap<>();
        map.put("customerServiceContactsId", imId);
        APIManager.getInstance().getPickAPI().getfindCustomerServiceImAccount(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                successLiveData.postValue(true);
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    mImLiveData.postValue(response.body().getMsg());
                } else {
                    // 接口返回的是String类型
                    String dataImId = response.body().getData().toString();
                    mImbLiveData.postValue(dataImId);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });

    }
}
